package com.yami.shop.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yami.shop.bean.dto.FlowPageAnalysisDto;
import com.yami.shop.bean.model.FlowPageAnalyseUser;
import com.yami.shop.bean.model.FlowPageAnalysis;
import com.yami.shop.dao.FlowPageAnalyseUserMapper;
import com.yami.shop.service.FlowPageAnalyseUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 流量分析—页面数据统计表
 *
 * @author YXF
 * @date 2020-07-16 16:23:53
 */
@Service
@AllArgsConstructor
public class FlowPageAnalyseUserServiceImpl extends ServiceImpl<FlowPageAnalyseUserMapper, FlowPageAnalyseUser> implements FlowPageAnalyseUserService {

    private final FlowPageAnalyseUserMapper flowPageAnalyseUserMapper;

    @Override
    public void insertBatch(List<FlowPageAnalysis> flowPageAnalysisList, List<FlowPageAnalysisDto> flowPageAnalysisDtoList) {
        FlowPageAnalyseUser flowPageAnalyseUser = null;
        List<FlowPageAnalyseUser> flowPageAnalyseUserList = Lists.newArrayList();
        for (int i = 0; i<flowPageAnalysisList.size();i++){
            FlowPageAnalysis flowPageAnalysis = flowPageAnalysisList.get(i);
            FlowPageAnalysisDto flowPageAnalysisDto = flowPageAnalysisDtoList.get(i);
            Set<String> userSet = new HashSet<>();
            //整理该页面中进行操作的所有用户
            userSet.addAll(flowPageAnalysisDto.getVisitUser());
            userSet.addAll(flowPageAnalysisDto.getVisitUser());
            userSet.addAll(flowPageAnalysisDto.getClickUser());
            userSet.addAll(flowPageAnalysisDto.getShareVisitUser());
            userSet.addAll(flowPageAnalysisDto.getPlaceOrderUser());
            userSet.addAll(flowPageAnalysisDto.getPayUser());
            for(String user: userSet){
                flowPageAnalyseUser = new FlowPageAnalyseUser();
                flowPageAnalyseUser.setFlowPageAnalyseId(flowPageAnalysis.getFlowPageAnalyseId());
                flowPageAnalyseUser.setUuid(user);
                flowPageAnalyseUser.setBizDate(flowPageAnalysis.getBizDate());
                flowPageAnalyseUser.setBizType(flowPageAnalysis.getBizType());
                flowPageAnalyseUser.setCreateDate(flowPageAnalysis.getCreateDate());
                flowPageAnalyseUser.setPageId(flowPageAnalysis.getPageId());
                flowPageAnalyseUser.setType(flowPageAnalysis.getType());
                //用户访问
                flowPageAnalyseUser.setIsVisit(getValue(flowPageAnalysisDto.getVisitUser(), user));
                //用户点击
                flowPageAnalyseUser.setIsClick(getValue(flowPageAnalysisDto.getClickUser(), user));
                //用户分享访问
                flowPageAnalyseUser.setIsShareVisit(getValue(flowPageAnalysisDto.getShareVisitUser(), user));
                //用户下单
                flowPageAnalyseUser.setIsPlaceOrder(getValue(flowPageAnalysisDto.getPlaceOrderUser(), user));
                //用户支付
                flowPageAnalyseUser.setIsPay(getValue(flowPageAnalysisDto.getPayUser(), user));
                //加购
                flowPageAnalyseUser.setIsPlusShopCart(getValue(flowPageAnalysisDto.getPlusShopCartUser(), user));
                flowPageAnalyseUserList.add(flowPageAnalyseUser);
            }
        }
        saveBatch(flowPageAnalyseUserList);
    }

    public Integer getValue(Set<String> set, String userId) {
        if (set.contains(userId)){
            return 1;
        }
        return 0;
    }
}
